package cn.remex.core.util.excelutils;

import cn.remex.core.exception.ServiceCode;
import cn.remex.core.util.Assert;
import cn.remex.core.util.Judgment;
import cn.remex.core.util.excelutils.bean.ExcelForReadInside;
import cn.remex.core.util.excelutils.bean.ExcelRead;
import cn.remex.core.util.excelutils.bean.ExcelReadRule;

import java.util.*;

/**
 * excel 解析数据
 * Created by guoqi on 2016/8/2.
 */
public class ExcelAnalysisUtil {
    //生成简单的excel　读取对象
    public static List<ExcelReadRule> obtainSimpleExcelReadRule(String[] excelTitle, String[] outTitle) {
        Assert.isTrue(excelTitle != null && outTitle != null, ServiceCode.FAIL, "创造excel读取规则时，excelTitle与outTitle 不能为空！");
        Assert.isTrue(excelTitle.length == outTitle.length, ServiceCode.FAIL, "创造excel读取规则时，excelTitle与outTitle 长度不相等");
        List<ExcelReadRule> excelReadRules = new ArrayList<>();
        for (int i = 0; i < excelTitle.length; i++) {
            ExcelReadRule excelReadRule = new ExcelReadRule(excelTitle[i], outTitle[i]);
            excelReadRules.add(excelReadRule);
        }
        return excelReadRules;
    }


    //解析excel 数据 并且
//    public static List<Map<String, Object>> excelHandleReadData(ExcelRead excelRead, List<ExcelReadRule> excelReadRules) {
//        Assert.notNull(excelRead, ServiceCode.FAIL, "数据错误！");
//        String[] titles = excelRead.getTitles();
//        Assert.isTrue(titles != null && titles.length > 0, ServiceCode.FAIL, "标题错误！");
//        List<String[]> datas = excelRead.getList();
//        Assert.isTrue(datas != null && datas.size() > 0, ServiceCode.FAIL, "没有数据！");
//        List<ExcelForReadInside> excelForReadList = new ArrayList<>();
//        excelReadRules.forEach(excelReadRule -> { //中转，添加了一个index 参数
//            Assert.isTrue(!Judgment.nullOrBlank(excelReadRule.getExcelTitle()) && !Judgment.nullOrBlank(excelReadRule.getOutTitle()), ServiceCode.FAIL, "传入转换标题错误！");
//            ExcelForReadInside excelForRead = new ExcelForReadInside();
//            excelForRead.setExcelTitle(excelReadRule.getExcelTitle());
//            excelForRead.setOutTitle(excelReadRule.getOutTitle());
//            excelForRead.setCanNull(excelReadRule.isCanNull());
//            excelForRead.setBiFunction(excelReadRule.getBiFunction());
//            excelForRead.setMaxFunction(excelReadRule.getMaxFunction());
//            excelForReadList.add(excelForRead);
//        });
//        List<String> stringList = Arrays.asList(titles);
//        excelForReadList.forEach(excelForReadInside -> { //获取标题的 index 位置
//            Optional<String> optional = stringList.stream().filter(s -> s.equals(excelForReadInside.getExcelTitle())).findFirst();
//            Assert.isTrue(optional.isPresent(), ServiceCode.FAIL, "缺少标题：" + excelForReadInside.getExcelTitle());
//            excelForReadInside.setDataIndex(stringList.indexOf(optional.get()));
//        });
//        List<Map<String, Object>> outers = new ArrayList<>();
//        for (String[] data : datas) {
//            Map<String, Object> outMap = new HashMap<>();
//            List<Map<String, String>> errorMapList = new ArrayList<>();
//            excelForReadList.forEach(excelForRead -> {
//                String dataStr = data[excelForRead.getDataIndex()];
//                Map<String, String> errorMap = new HashMap<>();
//                if (!excelForRead.isCanNull() && Judgment.nullOrBlank(dataStr)) {
//                    errorMap.put("excelErrorCode", excelForRead.getOutTitle());
//                    errorMap.put("excelErrorTitle", excelForRead.getExcelTitle());
//                    errorMap.put("excelErrorMsg", "这个字段不能为空！");
//                } else {
//                    String errorM = null;
//                    if (!Judgment.nullOrBlank(excelForRead.getBiFunction())) {
//                         errorM = excelForRead.getBiFunction().apply(dataStr, data);
//                    }else if (!Judgment.nullOrBlank(excelForRead.getMaxFunction())){
//                         errorM = excelForRead.getMaxFunction().domax(dataStr,excelForRead.getDataIndex(),data);
//                    }
//                    if (!Judgment.nullOrBlank(errorM)) {
//                        errorMap.put("excelErrorCode", excelForRead.getOutTitle());
//                        errorMap.put("excelErrorTitle", excelForRead.getExcelTitle());
//                        errorMap.put("excelErrorMsg", errorM);
//                    }
//                }
//                if (!errorMap.isEmpty())
//                    errorMapList.add(errorMap);
//                outMap.put(excelForRead.getOutTitle(), dataStr);
//            });
//            outMap.put("errorCell", errorMapList);
//            outers.add(outMap);
//        }
//        return outers;
//    }

}
